package com.eve.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** 
 * @ClassName: CommonUtil 
 * @Description: TODO 工具类
 */
public class CommonUtil {
	
	/**
	 * 验证输入字符串是否全为汉字和字母和下划线等,不能包含特殊字符
	 * @param nameValue 需要校验的字符串
	 * ***/
	public static boolean nameIsOk(String nameValue){
	    Pattern pattern = Pattern.compile("^[0-9a-zA-Z\\（\\）_()\u4e00-\u9FA5\\-\\s]+$");
	    Matcher m = pattern.matcher(nameValue);
        return m.matches();
    }
	
	/**
	 *  字符串中的连续空格和其他空白字符替换为单个空格
	 * @param nameValue 需要处理的字符串
	 * @return 
	 * ***/
	public static String replaceSpace(String nameValue){
		if(StringUtils.isNotBlank(nameValue)){
		   nameValue=nameValue.trim();
		   nameValue=nameValue.replaceAll("\\s+", " ");
		}
		 return nameValue;
	}

    public static String trimForward(String str, int length){
        if(StringUtils.isBlank(str)){
            return str;
        }

        int messageLength = str.length()>length?length:str.length();
        return str.substring(0, messageLength);
    }

    public static String trimBehind(String str, int length){
        if(StringUtils.isBlank(str) || str.length() < length){
            return str;
        }

        return str.substring(str.length()-length, str.length());
    }

    public static String fillString(String str, int totalLen)
    {
        if(StringUtils.isBlank(str)){
            return str;
        }

        String tmp = "";
        int len = str.length();
        if (len < totalLen)
        {
            for (int i = 0; i < totalLen - len; i++)
            {
                tmp += "0";
            }
            str = tmp + str;
        }
        return str;
    }


    /**
	 * 判断是否数据
	 * @param str
	 * @return true/false
	 */
	public static boolean isNumeric(String str) {
		if(StringUtils.isBlank(str)) return false;
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))&& str.charAt(i) !='.' && '-'!= str.charAt(i)) {
				return false;
			}
		}
		return true;
	}
	
	public static String getRandomCode(int codeLength){
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < codeLength; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sb.append(rand);
		}
		return sb.toString();
	}

    public static String getOrderNumber() {
        String date = DateHelper.dateToString(new Date(System.currentTimeMillis()), "yyMMddHHmmssS");
        return date + CommonUtil.getRandomCode(4);
    }


    public static void main(String[] args) {
        System.out.println( CommonUtil.trimForward("1", 1));
        System.out.println(fillString("sdfs", 2));
    }

    public static Long parseLong(String arg){
        try {
            return Long.parseLong(arg);
        }catch (NumberFormatException e){
            return 0l;
        }
    }

}
